/**
 * jims
 */
package com.jims.master.modules.sync.service;

import com.jims.master.common.persistence.Page;
import com.jims.master.common.service.CrudService;
import com.jims.master.common.utils.PrintPdfUtils;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.common.web.impl.BaseDto;
import com.jims.master.modules.sync.dao.*;
import com.jims.master.modules.sync.entity.*;
import com.jims.master.modules.sys.dao.CompanyDao;
import com.jims.master.modules.sys.dao.OrgDeptDao;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.DictUtils;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 检查申请Service
 * @author 陈晓阳
 * @version 2017-03-14
 */
@Service
@Transactional(readOnly = true)
public class ExamApplyService extends CrudService<ExamApplyDao, ExamApply> {

    @Autowired
    private ExamItemService examItemService;//检查申请明细Dao
    @Autowired
    private ClinicItemDictDao clinicItemDictDao;//诊疗项目Dao
    @Autowired
    private ClinicMasterService clinicMasterService;//门诊就诊记录service
    @Autowired
    private ExamItemLocationDao examItemLocationDao;



    /**
     * 保存当前患者新开的检查申请
     * @return
     * @Author 陈晓阳
     */
    /*@Transactional(readOnly = false)
    public int saveExamApplyData(ExamApply examApply,String clincItemIds) {
        ExamApply examApply1 = saveExamApply(examApply);
        int result=0;
        //2 保存子表
        if (examApply1!=null) {
            result = saveExamItems(examApply1,clincItemIds);//多条记录 对应诊疗项目
        }
        return result;
    }*/

    @Transactional(readOnly = false)
    public ExamApply saveexamApply(ExamApply examApply) {
        ClinicMaster clinicMaster = clinicMasterService.get(examApply.getClinicId());
        String testNo = "JC" + clinicMaster.getClinicNo() + (int) (Math.random() * 9000);
        examApply.setApplyOn(testNo);//申请序号
        examApply.setConfirmFlag("0");//确认标志
        examApply.setChargeFlag("0");
        this.save(examApply);
        return examApply;
    }

    @Transactional(readOnly = false,rollbackFor = Exception.class)
    public int saveExamApplyData(ExamApply examApply,String clincItemIds,String locactions) {
        ExamApply examApply1 =saveexamApply(examApply);
        int result = this.saveExamItem(examApply1,clincItemIds,locactions);//多条记录 对应诊疗项目
        return result;
    }


    /**
     * 保存病人申请检查子表记录多条数据
     * @return
     * @Author 陈晓阳
     */
    @Transactional(readOnly = false)
    public int saveExamItems(ExamApply examApply, String clincItemId){
        String[] clincItemIds=clincItemId.split(",");
        for(String clinicItemId:clincItemIds){
            String[] xm=clinicItemId.split("-");
            String id = xm[0];
            ClinicItemDict clinicItemDict = new ClinicItemDict();
            clinicItemDict.setId(id);
            clinicItemDict=clinicItemDictDao.getById(clinicItemDict);
            ExamItem saveData=new ExamItem();
            saveData.setApplyId(examApply.getId());
            saveData.setExamApply(examApply);
            saveData.setItemType("");
            saveData.setItemName(clinicItemDict.getItemName());
            saveData.setItemCode(clinicItemDict.getItemCode());
            saveData.setClinicItemId(clinicItemDict.getId());
            saveData.setCosts("");
            saveData.setOrgId(examApply.getOrgId());
           /* Double price=this.clinicItemDictDao.findPriceById(clinicItemDict.getId());
            saveData.setCosts((price!=null?price:0)+"");*/
            this.examItemService.save(saveData);

        }
        return clincItemIds.length;
    }



    @Transactional(readOnly = false)
    public int saveExamItem(ExamApply examApply, String clincItemId, String locactions){
        String[] clincItemIds=clincItemId.split(",");
        for(String clinicItemId:clincItemIds){
            String[] xm=clinicItemId.split("-");
            String id = xm[0];
            ClinicItemDict clinicItemDict = new ClinicItemDict();
            clinicItemDict.setId(id);
            clinicItemDict=clinicItemDictDao.getById(clinicItemDict);
            ExamItem saveData=new ExamItem();
            saveData.setApplyId(examApply.getId());
            saveData.setExamApply(examApply);
            saveData.setItemType(xm[1]);
            saveData.setItemName(clinicItemDict.getItemName());
            saveData.setItemCode(clinicItemDict.getItemCode());
            saveData.setClinicItemId(clinicItemDict.getId());
            saveData.setCosts("0");
            saveData.setOrgId(examApply.getOrgId());
           /* Double price=this.clinicItemDictDao.findPriceById(clinicItemDict.getId());
            saveData.setCosts((price!=null?price:0)+"");*/
            this.examItemService.save(saveData);
            if(locactions!=null&&!"".equals(locactions)){
            String[] locationIds = locactions.split(",");
            //京顺检查部位保存
            for (String locationId : locationIds) {
                String[] locations = locationId.split("-");
                if(locations.length==2&&locationId.contains(id)){
                    ExamItemLocation examItemLocation = new ExamItemLocation();
                    examItemLocation.setLocationId(locations[0]);
                    examItemLocation.setApplyId(saveData.getExamApply().getId());
                    examItemLocation.setItemId(saveData.getId());
                    examItemLocation.preInsert();
                    examItemLocation.setOrgId(saveData.getExamApply().getOrgId());
                    examItemLocationDao.insert(examItemLocation);
                }
            }
            }

        }
        return clincItemIds.length;
    }

   /**
     * <p>
     *     公共方法
     * </p>
     * @Param saveDataVos
     * 保存处置治疗表和代收费表
     * @return
     * @Author 陈晓阳
     */
   /* public Map<String,Object> commonSave(List<SaveDataVo> saveDataVos) {
        Map<String,Object> map=new HashMap<String, Object>();
        int resultVal=0;
        //1 保存代收费表 clinic_orders
        ClinicOrders cLinicOrders=this.saveClinicOrders(saveDataVos.get(0));
        //2 clinic_orders_cost
        resultVal+=this.saveClinicOrdersCosts(saveDataVos, cLinicOrders.getId());
        //3 保存处置治疗数据
        String treatId=this.saveOutoTreatRec(saveDataVos,cLinicOrders.getId());
        map.put("rows",resultVal);
        map.put("ordersId",cLinicOrders.getId());
        map.put("treatId",treatId);
        return map;
    }*/

    /**
     *查询门诊记录检查申请
     * @param clinicId
     * @return
     * @Author jict
     */
    public List<ExamApply> getByClinicId(String clinicId){
        return dao.getByClinicId(clinicId);
    }

    @Transactional(readOnly = false)
    public int deleteExam(String id){
        examItemService.delItem(id);
        int i = dao.deleteById(id);
        return i;
    }



}